package com.mediacloud.app.newsmodule.fragment.nav2;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.mediacloud.app.model.interfaces.DataInvokeCallBack;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.view.floatwin.FloatButtonGroup;
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapter;
import com.mediacloud.app.newsmodule.utils.NewsListDataInvoker;

import org.json.JSONObject;

/**
 * 来自api/content/list接口的列表数据
 * */
public abstract class RecyclerListFragmentDataFromContentList<R extends ArticleListData,T extends BaseRecyclerAdapter> extends BaseRecyclerListFragment<T> implements DataInvokeCallBack<R> {
    protected NewsListDataInvoker newsListDataInvoker;
    private R receiver;

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        pageIndex ++;
        newsListDataInvoker.getContentList(catalogID, pageIndex, perPageSize, needAd(),receiver );
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageIndex = 1;
        newsListDataInvoker.getContentList(catalogID, pageIndex, perPageSize, needAd(),receiver);
    }

    protected boolean needAd(){
        return true;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refreshLayout.autoRefresh(200);
        if (retryTips != null) {
            retryTips.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!currentState.equals(TYPE_LOADING)) {
                        showStateView(TYPE_LOADING, false);
                        pageIndex = 1;
                        newsListDataInvoker.getContentList(catalogID, pageIndex, perPageSize, needAd(), receiver);
                    }
                }
            });
        }
    }

    protected abstract R createReceiver();

    @Override
    protected void initOther() {
        super.initOther();
        receiver = createReceiver();
        newsListDataInvoker=new NewsListDataInvoker(this);
        refreshLayout.autoRefresh();
    }

    @Override
    public void success(R data) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        boolean haveComponent=false;
        if(pageIndex == 1){
            JSONObject floatData = data.orginData.optJSONObject("data");
            if (FloatButtonGroup.haveFloatField(floatData)) {
                initFloatWin(floatData);
            } else {
                hideFloatWin();
            }
            haveComponent = data.componentItems.size() != 0;
            try {
                addComponent(data.componentItems);
            } catch ( Exception e) {
                e.printStackTrace();
            }
        }
        if(!data.state||data.articleList.size() == 0){
            recyclerView.notifyDataSetChanged();
            if(pageIndex==1)
            {
                if(!haveComponent)
                {
//                    ViewUtils.showSnackBar(mRootView, "没有获取到数据")
                    showStateView(TYPE_NO_CONTENT, false);
                }
                else
                {
                    closeStateView();
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
                return;
            }
        }
        if(pageIndex == 1){
            recyclerAdapter.setData(data.articleList);
            if(recyclerAdapter.getData().isEmpty()){
                showStateView(TYPE_NO_CONTENT, false);
            } else{
                closeStateView();
            }
            recyclerView.notifyDataSetChanged();
        }else {
            recyclerAdapter.addAll(data.articleList);
            recyclerView.notifyDataSetChanged();
        }

    }

    @Override
    public void fault(Object data) {
        if(getActivity()==null||isDetached())
            return;
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        pageIndex-=1;
        showStateView(TYPE_NO_CONTENT, false);
//        if (data != null && pageIndex == 0) {
//            showStateView(TYPE_NET_NOT_GIVE_FORCE, false);
//        } else {
//            showStateView(TYPE_NO_CONTENT, false);
//        }
    }
}